package com.sc.site.article;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;














import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.common.pagination.Page;
import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.article.ArticleMapper;
import com.sc.dal.model.article.Article;
import com.sc.dal.model.consultComment.ConsultComment;
import com.sc.dal.model.member.Member;
import com.sc.service.article.ArticleService;
import com.sc.service.consultComment.ConsultCommentService;
import com.sc.site.BaseController;
import com.sc.util.data.Common;
import com.sc.util.data.DataUtil;
import com.sc.util.data.SessionContainer;
import com.sc.util.exception.ExceptionEnum;
@Controller
@RequestMapping(value="/article")
public class ArticleController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ConsultCommentService consultCommentService;
	
   @RequestMapping(value="/getArticleByid")
	public String getArticleByid(HttpServletRequest request,Integer aId,Model model){
		try {
			Member member = SessionContainer.getSessionMember(request);
			Article article1=new Article();
			Article article=articleService.selectByPrimaryKey(aId);
			if(article.getPageView()==null){
				article.setPageView(0);
			}
			article1.setPageView(article.getPageView()+1);
			article1.setId(article.getId());
			articleService.updateByPrimaryKeySelective(article1);
			model.addAttribute("article", article);
			ArticleTypeEnum[] aTypes=ArticleTypeEnum.values();
			model.addAttribute("atype",aTypes[Integer.parseInt(article.getAtype())-1].getDesc() );
			/**展现于看了又看的商品***/
			Map<String, Object> classMap = new HashMap<String, Object>();
			if(member!=null){
				String meberId = member.getmId();
				if(Common.isNotEmpty(meberId))
					classMap.put("creater", meberId);
			}
			List<ConsultComment> commentList = consultCommentService.queryLatelyTracks(classMap);
			model.addAttribute("seeGoodsList", commentList);
			return "/about/news";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "/about/store_trends";
		}
	}
	
	
	/**
	 * 分页查询商城动态文章信息
	 * <h2>方法注释说明</h2> 
	 * 
	 * @param pageNow
	 * @param pageSize
	 * @param atype
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping(value="/getArticleList")
	@ResponseBody
	public Map<String, Object> getArticleList(Integer pageNow,Integer pageSize,Integer atype){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		ArticleTypeEnum[] aTypes=ArticleTypeEnum.values();
		List<String[]> typeList=new ArrayList<String[]>();
		for (ArticleTypeEnum articleTypeEnum : aTypes) {
			String[] strArr=new String[2];
			strArr[0]=articleTypeEnum.getValue();
			strArr[1]=articleTypeEnum.getDesc();
			typeList.add(strArr);
		}
		Page page = new Page();
		// 传入页号
		page.setCurrPage(pageNow);
		// 传入每页记录数
		page.setPageSize(pageSize);
		
		Article article=new Article();
		// 添加查询条件
		article.setAtype(""+atype);
		if(atype==0){
			article.setColumn("page_view");
		}
		List<Article> resultList = new ArrayList<Article>();
		int totalRecords = 0;
		int totalPage = 0;
		try {
			if(pageNow == null || pageSize == null){				
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
				return resultMap;
		    }
			PaginationBean<Article> result = articleService.getPagingDatas(article,
					page, "count", "selectByParam",ArticleMapper.class);
			resultList = result.getPageList();
		
			totalRecords = result.getTotalRecords();
			totalPage = result.getTotalPage();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("分页查订单商品异常！");
		
		}
		resultMap.put("count", totalRecords);
		resultMap.put("pages", totalPage);
		resultMap.put("data", resultList);
		resultMap.put("typeData", typeList);
		return resultMap;
		
	}
}
